package cn.airfei.aircore.testcore.services.service.impl;

import cn.airfei.aircore.testcore.storages.convertor.StockConvertor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.airfei.aircore.testcore.services.domainService.impl.StockDomainServiceImpl;
import cn.airfei.aircore.testcore.storages.entity.Stock;
import cn.airfei.aircore.testcore.storages.dto.StockDto;
import cn.airfei.aircore.testcore.storages.vo.StockVo;
import cn.airfei.aircore.testcore.services.service.IStockService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import cn.airfei.aircore.core.storage.PageParam;
import cn.airfei.aircore.core.enums.RespEnum;
import cn.airfei.aircore.core.exception.AppException;
import cn.airfei.aircore.core.utils.QueryParamUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * <p>
 * 库存
 * </p>
 *
 * @author air
 * @since 2022-04-25
 */
@Service
public class StockServiceImpl implements IStockService {
    @Resource
    private StockConvertor stockConvertor;

    @Resource
    private StockDomainServiceImpl stockDomainService;

    /**
     *
     * @param stockDto
     * @param pageParam
     * @return
     */
    @Override
    public Page<StockVo> index(StockDto stockDto,PageParam pageParam) {
        return stockDomainService.index(stockDto,pageParam);
    }

    /**
     * 分页查询单表数据
     *
     * @param map
     * @return
     */
    @Override
    public Page<StockVo> singleTablePage(Map<String, Object> map) {
        Page<Stock> stockPage=stockDomainService.singleTablePage(map);
        List<StockVo> stockVoList = stockConvertor.entityList2VoList(stockPage.getRecords());
        Page stockVoPage = stockPage;
        stockVoPage.setRecords(stockVoList);
        return stockVoPage;
    }

    /**
     * 显示单条数据
     *
     * @param id
     * @return
     */
    @Override
    public StockVo show(String id){
        Stock stock = stockDomainService.show(id);
        return stockConvertor.entity2Vo(stock);
    }

    /**
     * 删除（销毁表数据）
     *
     * @param ids
     * @return
     */
    @Override
    public Integer destroy(String ids){
        return stockDomainService.destroy(ids);
    }

    /**
     * 更新表数据
     * @param stockDto
     * @return
     */
    @Override
    public Integer update(StockDto stockDto){
        Integer res = stockDomainService.update(stockConvertor.dto2Entity(stockDto));
        if (res < 1) {
            throw new AppException(RespEnum.UPDATE_FAIL.getCode(), RespEnum.UPDATE_FAIL.getMsg());
        }
        return res;
    }

    /**
     * 保存表数据
     *
     * @param {entity?uncap_first}Dto
     * @return
     */
    @Override
    public Integer store(StockDto stockDto){
        return stockDomainService.store(stockConvertor.dto2Entity(stockDto));
    }

    /**
     * 设置状态，1:正常,2:禁用/待审核，-1:已删除
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public boolean setStatus(Long id, Integer status) {
        if(stockDomainService.setStatus(id,status)>0){
            return true;
        }
        throw new AppException(RespEnum.SET_STATUS_ERROR.getCode(),RespEnum.SET_STATUS_ERROR.getMsg());
    }

    /**
     * get all resource in storage
     *
     * @return
     */
    @Override
    public List<StockVo> getList(Map<String,Object> map) {
        QueryWrapper<Stock> queryWrapper = QueryParamUtil.getEntityWrapper(map, Stock.class);
        return stockConvertor.entityList2VoList(stockDomainService.list(queryWrapper));
    }

    /**
     * 更新多条数据（主键id必传）
     *
     * @return
     */
    @Override
    public List<StockVo> updateBatch(List<StockDto> stockDtoList){
        List<Stock> stockList = stockConvertor.dtoList2EntityList(stockDtoList);
        stockDomainService.updateBatchById(stockList);
        return stockConvertor.entityList2VoList(stockList);
    }

     /**
      * 添加多条数据
      *
      * @return
      */
    @Override
    public List<StockVo> saveBatch(List<StockDto> stockDtoList){
        List<Stock> stockList = stockConvertor.dtoList2EntityList(stockDtoList);
        stockDomainService.saveBatch(stockList);
        return stockConvertor.entityList2VoList(stockList);
    }


}
